<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加策略')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table ref="multipleTable" size='mini' :data="blackFileList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="id" label="ID" align='center' width="100">
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column prop="name" align='center' :label="$t('策略名称')" width="200">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.name" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="desc" align='center' :label="$t('描述')" width="300">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.desc" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="rule_map" align='center' :label="$t('规则集')">
        <template slot-scope="scope">
          <tooltip :html="showHtmlBlack(scope.row.rule_map)" :content="showBlack(scope.row.rule_map)" class="wid190" :refName="scope.row.proto"></tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="ref" align='center' :label="$t('引用计数')" width="100"></el-table-column>
      <el-table-column align='center' width='100' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <black-file-dialog v-if="blackFileDialogVisible" ref="refBlackFileDialogVisible"></black-file-dialog>
  </div>
</template>
<script>
import { getBlacklistFile, delBlackInfo } from '@/api/safe/black'
import BlackFileDialog from './BlackFileDialog.vue'
import typeArray from './typeArray'
export default {
  name: 'BlackFileList',
  components: {
    BlackFileDialog
  },
  data () {
    return {
      activeName: 'first',
      blackFileDialogVisible: false,
      typeAllList: typeArray,
      blackFileList: [],
      selectList: [],
      nameStr: '',
      actionList: { 'alert': this.$t('告警'), 'deny': this.$t('阻断') },
      dirList: { 'upload': this.$t('上传'), 'download': this.$t('下载'), 'both': this.$t('双向') }
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      this.selectList = data
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      getBlacklistFile().then(res => {
        if (res.status === 0) {
          this.blackFileList = res.info
        }
      })
    },
    // 添加按钮
    dialogVisible (data) {
      this.blackFileDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refBlackFileDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delBlackInfo({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    showHtmlBlack (ruleMap) {
      let reStr = ''
      ruleMap.forEach(item => {
        if (item.switch === 'enable') {
          if (item.type === '0') {
            reStr += item.proto + ':' + this.dirList[item.dir] + ':' + this.actionList[item.action] + ':'
            var list = []
            this.typeAllList.forEach(val => {
              list.push(val.type)
            })
            reStr += list.join(',') + ';<br/>'
          } else if (item.type === '500') {
            reStr += item.proto + ':' + this.dirList[item.dir] + ':' + this.actionList[item.action] + ':;<br/>'
          } else {
            reStr += item.proto + ':' + this.dirList[item.dir] + ':' + this.actionList[item.action] + ':' + item.type + ';<br/>'
          }
        }
      })
      return reStr
    },
    showBlack (ruleMap) {
      let reStr = ''
      ruleMap.forEach(item => {
        if (item.switch === 'enable') {
          if (item.type === '0') {
            reStr += item.proto + ':' + this.dirList[item.dir] + ':' + this.actionList[item.action] + ':'
            var list = []
            this.typeAllList.forEach(val => {
              list.push(val.type)
            })
            reStr += list.join(',') + ';'
          } else if (item.type === '500') {
            reStr += item.proto + ':' + this.dirList[item.dir] + ':' + this.actionList[item.action] + ':;'
          } else {
            reStr += item.proto + ':' + this.dirList[item.dir] + ':' + this.actionList[item.action] + ':' + item.type + ';'
          }
        }
      })
      return reStr
    }
  }
}
</script>
